<?php
namespace Kxdr\Model;
use Think\Model\ViewModel;
class EventSendLogViewModel extends ViewModel {
	public $sid;
	public $eid;
	public $uid;
	public $wharr;
   public $viewFields = array(
	 'sendlog'=>array('_table'=>'sm_gifts_sendlog','id','kxdr_uid','shop_uid','sys_uid','ugid','egid','user_nick','gift_id','send_time','is_auto_send','sender_nick','nationality','province','city','city_part','address','true_name','mobile','wlname','wlid','wlcode','version','_type'=>'LEFT'),
	 'user_gift'=>array('_table'=>'sm_kxdr_user_gifts','id'=>'user_gift_id', 'eid', 'egid','epid','guess_id','user_id','gift_id','player_id','is_picup','picup_time','gift_num','gift_type','get_type','user_nick','gift_name','gift_pic_url','_on'=>'sendlog.ugid=user_gift.id','_type'=>'LEFT'),
	 );
   public function getEventSendLog(){
	    // unset($this->viewFields["player"]);
		$players = $this->where($this->wharr)->limit($this->lmtstr)->getField("id,send_time,is_auto_send,sender_nick,nationality,province,city,city_part,address,true_name,mobile,wlname,wlcode,gift_name,gift_pic_url");
		return $players;
   }
   public function getTotal(){
	   return $this->where($this->wharr)->limit($this->lmtstr)->count();
   }
   public function send_log_download(){
	   $data = $this->getEventSendLog();
	   $str = "物流名称,物流单号,收件人姓名,收件人手机,收件省,收件市,收件区/县,收件人地址,奖品名,时间,备注\r\n";   
		foreach($data as $k=>$gift){
			$str.=$data[$k]["wlname"].","
				 .$data[$k]["wlcode"].","
				 .$data[$k]["true_name"].","
				 .$data[$k]["mobile"].","
				 .$data[$k]["province"].","
				 .$data[$k]["city"].","
				 .$data[$k]["city_part"].","
				 .$data[$k]["address"].","
				 .$data[$k]["gift_name"].","
				 .date("Y-m-d H:i:s",strtotime($data[$k]["send_time"])).","
				 ."\r\n";
		}
		$filename = "发奖记录".date('Ymd').'.csv'; //设置文件名   
		// header("Content-type: application/vnd.ms-excel; charset=gb2312" );
		header("Content-type:text/csv");   
		header("Content-Disposition:attachment;filename=".$filename);   
		header('Cache-Control:must-revalidate,post-check=0,pre-check=0');   
		header('Expires:0');   
		header('Pragma:public');
		
		echo mb_convert_encoding($str,"gb2312","utf-8");  
   }
}